package com.groupfifteen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.groupfifteen.common.R;
import com.groupfifteen.entity.*;
import com.groupfifteen.service.*;
import com.groupfifteen.util.ApiResultHandler;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("syspaper/manage")
@RequiredArgsConstructor
public class SysPaperManageController {

    @Autowired
    private SysPaperManageService sysPaperManageService;
    private final SysPaperManageServiceImpl paperService;

    private final SysJudgeQuestionServiceImpl judgeQuestionService;

    private final SysMultiQuestionServiceImpl multiQuestionService;

    private final SysFillQuestionServiceImpl fillQuestionService;

    @PostMapping("/query")
    public IPage<SysPaperManageEntity> querySysPaperManages(@RequestBody SysPaperManageEntity sysPaperManageEntity,
                                                            @RequestParam(defaultValue = "1") int pageNo,
                                                            @RequestParam(defaultValue = "10") int pageSize) {
        return sysPaperManageService.QuerySysPaperManages(sysPaperManageEntity, pageNo, pageSize);
    }
    @PostMapping("addpaper")
    public String AddPaper(@RequestBody SysPaperManageEntity sysPaperManageEntity) {
        Integer result = sysPaperManageService.AddPaper(sysPaperManageEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    @DeleteMapping("/deletepaper/{paper_id}")
    public String DeletePaper(@PathVariable("paper_id") Integer paper_id) {
        Integer result = sysPaperManageService.DeletePaper(paper_id);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    @GetMapping("/getpaper/{paper_id}")
    public SysPaperManageEntity GetPaper(@PathVariable("paper_id") Integer paper_id) {
        SysPaperManageEntity result = sysPaperManageService.GetPaper(paper_id);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

    @PutMapping("/updatepaper/{paper_id}")
    public String UpdatePaper(@PathVariable("paper_id")Integer paper_id,@RequestBody SysPaperManageEntity sysPaperManageEntity) {
        sysPaperManageEntity.setPaperId(paper_id);
        Integer result = sysPaperManageService.UpdatePaper(sysPaperManageEntity);
        if (result > 0) {
            return "success";
        } else {
            return "fail";
        }
    }
    /**
     * 查询所有试卷
     *
     * @return 试卷
     */
    @GetMapping("/papers")
    public R<List<SysPaperManageEntity>> findAll() {
        return ApiResultHandler.buildApiResult(200, "请求成功", paperService.findAll());
    }

    /**
     * 通过试卷id 查询试卷
     *
     * @param paperId 试卷id
     * @return 试卷内容
     */
    @GetMapping("/paper/{paperId}")
    public Map<Integer, List<?>> findById(@PathVariable("paperId") Integer paperId) {
        //选择题题库 1
        List<SysMultiQuestionEntity> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);
        //填空题题库 2
        List<SysFillQuestionEntity> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);
        //判断题题库 3
        List<SysJudgeQuestionEntity> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);
        Map<Integer, List<?>> questionMap = new HashMap<>();
        questionMap.put(1, multiQuestionRes);
        questionMap.put(2, fillQuestionsRes);
        questionMap.put(3, judgeQuestionRes);

        return questionMap;
    }

    /**
     * 添加试卷
     *
     * @param paperManage 试卷内容
     * @return 结果
     */
    @PostMapping("/paperManage")
    public R<Integer> add(@RequestBody SysPaperManageEntity paperManage) {
        int res = paperService.add(paperManage);
        if (res != 0) {
            return ApiResultHandler.buildApiResult(200, "添加成功", res);
        }
        return ApiResultHandler.buildApiResult(400, "添加失败", res);
    }

    /**
     * 删除试卷中的某条试题
     *
     * @param paperId    试卷id
     * @param type       题目类型。1选择，2填空，3判断
     * @param questionId 题目id
     */
    @GetMapping("/paper/delete/{paperId}/{type}/{questionId}")
    public R delete(
            @PathVariable("paperId") String paperId,
            @PathVariable("type") String type,
            @PathVariable("questionId") String questionId
    ) {
        paperService.delete(paperId, type, questionId);
        return ApiResultHandler.buildApiResult(200, "删除成功", null);
    }


}